package com.zhss.redis.geo;

import redis.clients.jedis.GeoRadiusResponse;
import redis.clients.jedis.GeoUnit;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.Jedis;

import java.util.List;

/**
 * @Description TODO
 * @Author winston
 * @DateTime 2021/11/8
 */
public class NearByUserDemo {
	HostAndPort hostAndPort = new HostAndPort("121.4.79.149", 6379);
	Jedis jedis = new Jedis(hostAndPort);

	public static void main(String[] args) {
		NearByUserDemo demo = new NearByUserDemo();
		demo.setLocation("固始", 116.33322234343333, 39.33303039494949);
		demo.setLocation("张三", 116.33322234343333, 39.444333333);
		demo.setLocation("杭州", 117.33322234343333, 40.33303039494949);

		System.out.println(demo.getDistance("张三","固始"));
		demo.getNearByUsers("张三").forEach(i -> System.out.println(i.getMemberByString()));
	}

	public void setLocation(String name, double lgt, double lat) {
		jedis.geoadd("location_data", lgt, lat, name);
	}

	public double getDistance(String user, String shop) {
		return jedis.geodist("location_data", user, shop, GeoUnit.KM);
	}

	public List<GeoRadiusResponse> getNearByUsers(String name) {
		return jedis.georadiusByMember("location_data", name, 15, GeoUnit.KM);
	}

}
